// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package copyengine.xflDecode
{
    import copyengine.utils.debug.CELog;
    import copyengine.xflDecode.meta.DOMElementMeta;
    import copyengine.xflDecode.meta.DOMFrameMeta;
    import copyengine.xflDecode.meta.DOMLayerMeta;
    import copyengine.xflDecode.meta.DOMTimeLineMeta;

    public class XFLAnalysis
    {
        public function XFLAnalysis()
        {
        }

        /**
         * 分析一个XFL文件(Flash IDE存储的xml) 并将其转化为一个DOMTileLineMeta对象
         */
        public static function analyze(_xml:XML):DOMTimeLineMeta
        {
            var timeLineMeta:DOMTimeLineMeta = new DOMTimeLineMeta();

            timeLineMeta.name = _xml.@name;
            timeLineMeta.linkageClassName = _xml.@linkageClassName;
            timeLineMeta.layers = new Vector.<DOMLayerMeta>();

            //TimeLine
            var timeLineNode:XML = _xml.children()[0];
            var domTimeLineNode:XML = timeLineNode.children()[0];

            //Layers
            var layersNode:XML = domTimeLineNode.children()[0];
            var domLayerNodeList:XMLList = layersNode.children();
            var totalLayersNum:int = domLayerNodeList.length();

            for (var layerIndex:int = 0; layerIndex < totalLayersNum; layerIndex++)
            {
                var layerMeta:DOMLayerMeta = new DOMLayerMeta();
                timeLineMeta.layers.push(layerMeta);
                doAnalyzeEachLayerNode(domLayerNodeList[layerIndex], layerMeta);
            }

            return timeLineMeta;
        }


        private static function doAnalyzeEachLayerNode(_layerNode:XML, _layerMeta:DOMLayerMeta):void
        {
            _layerMeta.frames = new Vector.<DOMFrameMeta>();
            var framesNode:XML = _layerNode.children()[0];
            var domFrameNodeList:XMLList = framesNode.children();
            var totalFrameNum:int = domFrameNodeList.length();
            for (var frameIndex:int = 0; frameIndex < totalFrameNum; frameIndex++)
            {
                var frameMeta:DOMFrameMeta = new DOMFrameMeta();
                _layerMeta.frames.push(frameMeta);
                doAnalyzeEachFrameNode(domFrameNodeList[frameIndex], frameMeta);
            }

        }


        private static function doAnalyzeEachFrameNode(_frameNode:XML, _frameMeta:DOMFrameMeta):void
        {
            _frameMeta.elements = new Vector.<DOMElementMeta>();

            var elementsNode:XML = _frameNode.children()[0];
            var domElementNodeList:XMLList = elementsNode.children();
            var totalElementNum:int = domElementNodeList.length();
            for (var elementIndex:int = 0; elementIndex < totalElementNum; elementIndex++)
            {
                var elementMeta:DOMElementMeta = new DOMElementMeta();
                _frameMeta.elements.push(elementMeta);
                doAnalyzeEachElementNode(domElementNodeList[elementIndex], elementMeta);
            }

        }

        private static function doAnalyzeEachElementNode(_elementNode:XML, _elementMeta:DOMElementMeta):void
        {
            var isCanAnalysis:Boolean = true;
            switch (_elementNode.localName())
            {
                case XFLDecodeConfig.ELEMENT_TYPE_SYMBOL:
                    _elementMeta.elementType = DOMElementMeta.TYPE_SYMBOL_INSTANCE;
                    break;
                case XFLDecodeConfig.ELEMENT_TYPE_BITMAP:
                    _elementMeta.elementType = DOMElementMeta.TYPE_BITMAP_INSTANCE;
                    break;
                default :
                    CELog.log("Ignore Element: " + _elementNode.localName());
                    isCanAnalysis = false;
                    break;
            }
            if (isCanAnalysis)
            {
                _elementMeta.libraryItemName = _elementNode.@libraryItemName;
                _elementMeta.name = _elementNode.@name;

                var matrixNode:XML = XFLAnalysisUtils.getPropertyNode(_elementNode, "matrix");
                if (matrixNode)
                {
                    matrixNode.hasOwnProperty("@a") ? _elementMeta.transformMatrixA = matrixNode.@a : null;
                    matrixNode.hasOwnProperty("@b") ? _elementMeta.transformMatrixB = matrixNode.@b : null;
                    matrixNode.hasOwnProperty("@c") ? _elementMeta.transformMatrixC = matrixNode.@c : null;
                    matrixNode.hasOwnProperty("@d") ? _elementMeta.transformMatrixD = matrixNode.@d : null;
                    matrixNode.hasOwnProperty("@tx") ? _elementMeta.transformMatrixTX = matrixNode.@tx : null;
                    matrixNode.hasOwnProperty("@ty") ? _elementMeta.transformMatrixTY = matrixNode.@ty : null;
                }

                var anchorNode:XML = XFLAnalysisUtils.getPropertyNode(_elementNode, "transformationPoint");
                if (anchorNode)
                {
                    anchorNode.hasOwnProperty("@x") ? _elementMeta.transformPointX = anchorNode.@x : null;
                    anchorNode.hasOwnProperty("@y") ? _elementMeta.transformPointY = anchorNode.@y : null;
                }

                var colorNode:XML = XFLAnalysisUtils.getPropertyNode(_elementNode, "color");
                if (colorNode)
                {
                    colorNode.hasOwnProperty("@alphaMultiplier") ? _elementMeta.alpha = colorNode.@alphaMultiplier : null;
                }
            }

        }


    }
}
